import axios from 'axios'
import store from '@/store'

const ConfigBaseURL = 'http://106.13.136.240:12333'

//不带token的请求
export const ServiceWithOutToken = axios.create({
    timeout: 5000,
    baseURL: ConfigBaseURL,
    method: 'post',
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
})
// 添加请求拦截器
ServiceWithOutToken.interceptors.request.use(config => {
    return config
})
// 添加响应拦截器
ServiceWithOutToken.interceptors.response.use(response => {
    // console.log(response)
    return response.data
}, error => {
    console.log('TCL: error', error)
    const msg = error.Message !== undefined ? error.Message : ''
    console.log(msg)
    alert("网络异常！");
    return Promise.reject(error)
})

//带token的请求
export const Service = axios.create({
    timeout: 5000,
    baseURL: ConfigBaseURL,
    method: 'post',
    headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        'x-auth-token': store.state.token
    }
})
Service.interceptors.request.use(config => {
    return config
})
Service.interceptors.response.use(response => {
    if (response.data.code !== 0) {
        console.log("error")
    }
    return response.data
}, error => {
    console.log('TCL: error', error)
    const msg = error.Message !== undefined ? error.Message : ''
    console.log(msg)
    alert("网络异常！");
    return Promise.reject(error)
})
